<?php

namespace app\common\model;

use think\facade\Session;
use think\Model;

class PayLog extends Model
{
    /**
     * 充值提成表
     */
    

    /**
     * 获取充值记录列表 api
     */
    public function getuserlist($page, $limit, $key)
    {
        if (isset($key)) {
            $cate_list = $this->where('username', 'like', "%$key%")->limit($page, $limit)->order('Id', 'desc')->select();
            $list_number = $this->where('username', 'like', "%$key%")->count();
        } else {
            $cate_list = $this->limit($page, $limit)->order('Id', 'desc')->select();
            $list_number = $this->count();
        }
        $msg = '';
        if ($cate_list->isEmpty()) {
            $msg = '暂无数据';
        }
        return json_l($cate_list->toArray(), $list_number, $msg);
    }

    /**
     * 获取当前用户的提成表
     * @return void
     */
     public function getList(){
        $uid = Session::get('main_user')['Id'];
        $list = $this->where('invite',$uid)->limit(15)->select();
        return $list;
    }

    /**
     * 获取该用户已有效邀请人数
     * @return void
     */
    public function getUserNum(){
        $uid = Session::get('main_user')['Id'];
        $num = $this->where('invite',$uid)->count();
        return $num;
    }
    /**
     * 插入充值记录
     * @return void
     */
    public function in_log($pay_order,$uid,$balance,$type){
        $u = User::where('Id',$uid)->find();
        $data=[
            'pay_order'=>$pay_order,
            'username'=>$u['username'],
            'userid'=>$u['Id'],
            'invite'=>$u['invite'],
            'type'=>$type,
        ];
        if($type == 3){
            $data['state']=1;
            $data['balance']= $balance;
        }else{
            $data['state']=0;
            $data['balance']=Get_Ratio($balance);
        }
        if($this->isUpdate(false)->allowField(true)->save($data)){
            return true;
        }
        return false;
    }

    /**
     * 获取某个用户未结算的金额
     * @return void
     */
    public function pop_info_0($id){
        $num = $this->where('invite',$id)->where('state',0)->sum('balance');
        $num = $num / 10;
        return $num;
    }
    /**
     * 获取某个用户已结算的金额
     * @return void
     */
    public function pop_info_1($id){
        $num = $this->where('invite',$id)->where('state',1)->sum('balance');
        $num = $num / 10;
        return $num;
    }

    /**
     * 结算佣金
     * @return void
     */
    public function pop_settle($uid){
        if($this->where('invite',$uid)->setField('state',1)){
            return true;
        }
        return false;
    }


}
